package com.kgc.crowd.distributioncrowdcommon.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Collection;
import java.util.UUID;

public class CrowdUtils {
    /**
     * 验证集合是否有效
     * @param c       待验证集合
     * @return    验证结果（true：有效，false：无效）
     * @author 封捷
     */
    public static <E> boolean collectionEffectiveCheck(Collection<E> c) {
        return (c != null) && (c.size() > 0);
    }

    /**
     * 验证字符串是否有效
     * @param source   待验证字符串
     * @return       验证结果（true：有效，false：无效）
     * @author 封捷
     */
    public static boolean strEffectiveCheck(String source) {
        return (source != null) && (source.length() > 0);
    }

    /**
     * 生成随机验证码
     * @param length   验证码长度
     * @return       生成的验证码
     * @throws RuntimeException 验证码长度必须大于0
     * @author 封捷
     */
    public static String randomCode(int length) {

        if(length <= 0) {
            throw new RuntimeException(CrowdConstant.MESSAGE_RANDOM_CODE_LENGTH_INVALID);
        }

        StringBuilder builder = new StringBuilder();

        for(int i = 0; i < length; i++) {

            // 1.生成随机数
            double doubleRandom = Math.random();

            // 2.调整
            int integerRandom = (int) (doubleRandom * 10);

            // 3.拼接
            builder.append(integerRandom);
        }

        return builder.toString();
    }

    /**
     * 发送验证码短信
     * @param randomCode   验证码值
     * @param phoneNum    接收验证码短信的手机号
     */
    public static void sendShortMessage(String accessKeyId,String secret,String randomCode, String phoneNum){
// TODO Auto-generated method stub
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                accessKeyId,
                secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "风流倜傥");
        request.putQueryParameter("TemplateCode", "SMS_175584267");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+randomCode+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    /**
     * 生成用户登录成功后使用的token
     * @return
     */
    public static String generateToken() {

        return CrowdConstant.REDIS_MEMBER_SING_TOKEN_PREFIX + UUID.randomUUID().toString().replaceAll("-", "");
    }
    /**
     * 生成ProjectVO成功后使用的token
     * @return
     */
    public static String projectVOToken() {

        return CrowdConstant.REDIS_PROJECT_TEMP_TOKEN_PREFIX + UUID.randomUUID().toString().replaceAll("-", "");
    }
}
